/* $Id: ServiceRoleDAOImpl.java 
 * Copyright (C) 2011, TRAKHEES
 *  
 * Revision History
 *
 * Date  			Author 		    	Remarks
 * ---------------------------------------------------------------------------------
 *
 */
package ae.pcfc.etks.online.common.dao.provider;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Subqueries;
import org.springframework.stereotype.Repository;

import ae.pcfc.etks.online.common.dao.ServiceRoleDAO;
import ae.pcfc.etks.online.common.domain.Client;
import ae.pcfc.etks.online.common.domain.ClientServiceRole;
import ae.pcfc.etks.online.common.domain.ServiceRole;
import ae.pcfc.etks.online.common.domain.UserServiceSecurity;

@Repository
public class ServiceRoleDAOImpl extends BaseDAO implements ServiceRoleDAO {

	/**
	 * findServiceRoleByRoleIds
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ServiceRoleDAO#findServiceRoleByRoleIds(java.util.List)
	 * 
	 */
	public List<ServiceRole> findServiceRoleByRoleIds(List<Long> serviceRoleIds) {
		Criteria criteria = getHibernateTemplate().getSessionFactory()
				.getCurrentSession()
				.createCriteria(ServiceRole.class, "serviceRole");
		criteria.add(Restrictions.in("serviceRole.srId", serviceRoleIds));
		@SuppressWarnings("unchecked")
		List<ServiceRole> list = (List<ServiceRole>) criteria.list();

		return list == null ? null : list;
	}

	/**
	 * findServiceRoleByRoleId
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ServiceRoleDAO#findServiceRoleByRoleId(java.lang.Long)
	 * 
	 */
	public List<ServiceRole> findServiceRoleByRoleId(Long roleId) {

		Criteria criteria = getHibernateTemplate().getSessionFactory()
				.getCurrentSession()
				.createCriteria(ServiceRole.class, "serviceRole");
		criteria.add(Restrictions.eq("serviceRole.roleId", roleId));
		@SuppressWarnings("unchecked")
		List<ServiceRole> serviceRoles = (List<ServiceRole>) criteria.list();

		return (serviceRoles != null && serviceRoles.size() > 0) ? serviceRoles
				: null;
	}

	/**
	 * add
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ServiceRoleDAO#add(ae.pcfc.etks.online.common.domain.ServiceRole)
	 * 
	 */
	public void add(ServiceRole serviceRole) {

		getHibernateTemplate().getSessionFactory().getCurrentSession()
				.save(serviceRole);

	}

	/**
	 * findServiceRolesByServiceType
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ServiceRoleDAO#findServiceRolesByServiceType(java.lang.String)
	 * 
	 */
	public List<ServiceRole> findServiceRolesByServiceType(String serviceType) {
		// TODO Auto-generated method stub
		Criteria criteria = getHibernateTemplate().getSessionFactory()
				.getCurrentSession()
				.createCriteria(ServiceRole.class, "serviceRole");
		criteria.createAlias("serviceRole.service", "service");
		criteria.add(Restrictions.eq("service.serviceType", serviceType)
				.ignoreCase());
		criteria.add(Restrictions.eq("service.serviceStatus", "A"));
		@SuppressWarnings("unchecked")
		List<ServiceRole> list = (List<ServiceRole>) criteria.list();

		return list == null ? null : list;
	}

	/**
	 * findServiceRolesByUserName
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ServiceRoleDAO#findServiceRolesByUserName(java.lang.String)
	 * 
	 */
	public List<ServiceRole> findServiceRolesByUserName(String strUserName) {
		Criteria criteria = getHibernateTemplate().getSessionFactory()
				.getCurrentSession()
				.createCriteria(UserServiceSecurity.class, "uss");
		criteria.setProjection(Projections.property("uss.serviceRole"));
		criteria.createAlias("uss.serviceRole", "serviceRole");
		criteria.createAlias("serviceRole.service", "service");
		criteria.add(Restrictions.eq("service.serviceStatus", "A"));
		criteria.add(Restrictions.eq("uss.usSecurityUsername", strUserName)
				.ignoreCase());
		@SuppressWarnings("unchecked")
		List<ServiceRole> list = (List<ServiceRole>) criteria.list();

		return list == null ? null : list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ServiceRoleDAO#
	 * findServiceRoleByClientAndRoleId
	 * (ae.pcfc.etks.online.common.domain.Client, java.lang.Long)
	 */
	public List<ClientServiceRole> findServiceRoleByClientAndRoleId(
			Client client, Long roleId) {
		Criteria criteria = getHibernateTemplate().getSessionFactory()
				.getCurrentSession()
				.createCriteria(ClientServiceRole.class, "csr");
		criteria.createAlias("csr.serviceRole", "serviceRole");
		criteria.add(Restrictions.eq("serviceRole.srId", roleId));
		criteria.add(Restrictions.eq("client.CId", client.getCId()));
		@SuppressWarnings("unchecked")
		List<ClientServiceRole> list = (List<ClientServiceRole>) criteria
				.list();

		return list == null ? null : list;
	}

	public List<ServiceRole> findAvailableServiceRoleByRoleIdForClient(
			Long roleId, Client client) {
		Criteria criteria = getHibernateTemplate().getSessionFactory()
				.getCurrentSession()
				.createCriteria(ServiceRole.class, "serviceRole");
		criteria.add(Restrictions.eq("serviceRole.roleId", roleId));
		
		DetachedCriteria subCriteria = DetachedCriteria.forClass(ClientServiceRole.class, "csr");
		subCriteria.createAlias("csr.serviceRole", "csrServiceRole");
		subCriteria.setProjection(Projections.property("csrServiceRole.srId"));
		subCriteria.createAlias("csr.client", "client");
		subCriteria.add(Restrictions.eq("client.CId", client.getCId()));
		criteria.add(Subqueries.propertyNotIn("serviceRole.srId", subCriteria));

		@SuppressWarnings("unchecked")
		List<ServiceRole> list = (List<ServiceRole>) criteria.list();

		return list == null ? null : list;
	}

}
